import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/index',
    name: 'index',
    meta: { title:'index' },
    component: () => import('../views/Index.vue'),
    children:[
      {
        path: '/index/home',
        name: 'home',
        meta: { title:'首页' },
        component: () => import('../views/index/Home.vue')
      },
      {
        path: '/index/tuan',
        name: 'tuan',
        meta: { title:'爆爆团' },
        component: () => import('../views/index/Tuan.vue')
      },
      {
        path: '/index/order',
        name: 'order',
        meta: { title:'订单' },
        component: () => import('../views/index/Order.vue')
      },
      {
        path: '/index/mine',
        name: 'mine',
        meta: { title:'我的' },
        component: () => import('../views/index/Mine.vue')
      },
      {
        path:'/index',
        redirect:'/index/home'
      },
      {
        path:'*',
        component: () => import('../views/NotFound.vue') 
        
      }
    ]
    
  },
  {
    path: '/register',
    name: 'register',
    meta: { title:'注册' },
    component: () => import('../views/Register.vue'),
  },
  {
    path: '/login',
    name: 'about',
    meta: { title:'登陆' },
    component: () => import('../views/Login.vue'),
  },
  {
    // 路由地址中带有 :xxx 的路由地址 都属于动态路由
    // path: '/detail/:name', // :name 是个占位符, 实际在跳路由的时候,会动态拼接参数 例如: '/detail/'+100
    path: '/detail/:id',
    name: 'detail',
    meta: { title:'详情' },
    component: () => import('../views/Detail.vue'),
    
  },
  {
    path: '/collect',
    name: 'collect',
    meta: { title:'收藏' },
    component: () => import('../views/Collect.vue')
  },
  {
    path: '/address',
    name: 'address',
    meta: { title:'地址' },
    component: () => import('../views/Address.vue')
  },
  {
    path: '/addaddress',
    name: 'addaddress',
    meta: { title:'新增地址' },
    component: () => import('../views/Addaddress.vue')
  },
  {
    path: '/modaddress',
    name: '/modaddress',
    meta: { title:'修改地址' },
    component: () => import('../views/Modifyaddress.vue')
  },
  {
    path: '/search',
    name: 'search',
    meta: { title:'搜索' },
    component: () => import('../views/Search.vue'),
    children:[
      {
        path: '/search/home',
        name: 'home',
        meta: { title:'搜索页' },
        component: () => import('../views/search/s_home.vue')
      },
      {
        path: '/search/result',
        name: 'result',
        meta: { title:'搜索结果' },
        component: () => import('../views/search/s_result.vue')
      },
      // 二级路由重定向
      {
        path:'/search',
        redirect:'/search/home'
      },
      {
        path:'*',
        component: () => import('../views/NotFound.vue') 
        
      }
    ]
  },
  // 一级路由重定向
  {
    path:'/',
    redirect:'/index'
  },
  // 404路由
  {
    path:'*',
    component: () => import('../views/NotFound.vue') 
    
  }
]

// 4.实例化路由对象
const router = new VueRouter({
  mode: 'history', //设置路由模式, history , hash
  base: process.env.BASE_URL,
  routes
})

//设置实例方法( 导航守卫方法 )

//路由的全局前置守卫方法
router.beforeEach((to, from, next) => {
  //参数一: to 新的路由对象
  //参数二: from 旧的路由对象
  //参数三: next 路由控制方法, 调用该方法则允许路由跳转, 未调用该方法则不允许路由跳转

  //动态更新网页标题
  document.title = to.meta.title;

  //获取localStorage中的登陆凭证
  var token = localStorage.getItem('token');

  //只有在登陆以后 才可以跳转到 订单页
  // if(to.path == '/index/order'){ //想要跳往 订单页

  if(to.path == '/index/order' || to.path == '/index/mine'){ 
    //想要跳往 订单页 或者 我的页面
    if( token ){ //已经登陆
      next();
    }else{ //未登录
      next('/login');
    }
  }else{ //其他页面( 非订单页 )
    next();//必须手动调用该函数, 否则无法完成路由跳转
  } 
  
  //对于后台管理系统这种应用, 只有登录页是可以随意访问的, 但是其他所有页面都是必须在登陆以后才可以访问
  // if( to.path == '/register' ){ //跳往 注册页 , 允许跳转
  //   next();
  // }else{
  //   if( to.path == '/login' ){ //跳往 登录页
  //     if( token ){ //已登录 , 重定向到 "/"
  //       next('/');
  //     }else{ //未登录 , 允许跳往 登录页
  //       next(); 
  //     }
  //     // next();
  //   }else{ //跳往 非登录页
  //     if( token ){ //已登录 , 允许访问
  //       next(); 
  //     }else{ //未登录 , 重定向到 "/login"
  //       next('/login');
  //     }
  //   }
  // }
})

//路由的全局后置守卫方法
// router.afterEach((to,from)=>{
//   console.log('afterEach');
// })

export default router
